package com.example.springbootredis.service;
import com.example.springbootredis.dao.StudentDao;
import com.example.springbootredis.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {
    @Autowired
    private StudentDao studentDao;

    @Cacheable(value = "students", key = "'all_students'")
    public List<Student> getAllStudents() {
        List<Student> allStudents = studentDao.getAllStudents();
        return allStudents;
    }


    public Student getStudentById(int id) {
        return studentDao.getStudentById(id);
    }

    @CacheEvict(value = "students", key = "'student_' + #student.id")
    public void addStudent(Student student) {
        studentDao.addStudent(student);
    }

    @CacheEvict(value = "students", key = "'student_' + #student.id")
    public void updateStudent(Student student) {
        studentDao.updateStudent(student);
    }

    @CacheEvict(value = "students", key = "'student_' + #id")
    public void deleteStudent(int id) {
        studentDao.deleteStudent(id);
    }
}
